package levels
{
    import flash.display.*;
    import flash.geom.*;
    
    import logic.obstacles.*;

    public class LevelModel 
    {
        public var playerX:uint = 20;
        public var playerY:uint = 20;
        
        public var backgroundSprites:Array = [];        
        
        
        //Array of arrays [startX, startY, width, height]
        public var background:Array = [];
        
        //Array of enemies
        public var enemies:Array = [];
        
        public var levelWidth:Number = 640;
        public var levelHeight:Number = 640;
        
        private var constructedEnemies:Array = [];
        
        
        public function buildBackground():Array
        {
            var backgroundS:Sprite = new Sprite()
            backgroundS.visible = false;
            var i:uint;
            var j:uint
    
            for each (var model:Array in background)
            {
                if (model[0] == 0)
                {
                    backgroundS.graphics.beginFill(0x000000, 1)
                    backgroundS.graphics.drawRect(model[1],model[2], model[3], model[4]); 
                    backgroundS.graphics.endFill();

                    backgroundS.graphics.beginFill(0x000000, 1)
                    backgroundS.graphics.drawRect(model[1]-5,model[2], model[3]+10, 10); 
                    backgroundS.graphics.endFill();
                   
                    var numHoriz:Number = model[3]/50;
                    var numVert:Number = model[3]-30 / 50;
                    for (j=0;j<numVert-1; j++) {
                        for (i=0; i<numHoriz-1; i++) {                    
                            if (Math.random()<0.5)
                                backgroundS.graphics.beginFill(0xfcff00, 0.3*Math.random());
                            else
                                backgroundS.graphics.beginFill(0xffffff, 0.3*Math.random());
                                
                            backgroundS.graphics.drawRect(model[1]+( (model[3] - (numHoriz-1)*50 )-10)+i*50, 30+model[2]+j*60,20,40); 
                        }
                    }
                    backgroundS.graphics.endFill();
                } else if (model[0] == 1) {
                    
                    var grider:Sprite = new Sprite();
                    grider.graphics.beginFill(0x000000, 1);
                    grider.graphics.lineStyle(2,0x000000);
                    grider.graphics.drawRect(0,0,model[3],2);
                    grider.graphics.drawRect(0,10,model[3],2);
                    grider.graphics.endFill();
                    
                    var num:Number = model[3] / 10;
                    
                    for (i=0;i<model[3]; i+=10) {
                        grider.graphics.moveTo(i,1);
                        grider.graphics.lineTo(i+5,10);
                        grider.graphics.lineTo(i+10,0);
                    }
                    
                   
                    grider.x = model[1];
                    grider.y = model[2];
                    grider.rotation = model[4];
                    backgroundS.addChild(grider);
                    

                    backgroundS.graphics.endFill();
                    
                }else if (model[0] == 2) {
                    
                    var colours:Array = [   [0x5e0042, 0x2c2233, 0x005869, 0x00856a, 0x8db500],
                                            [0xff6300, 0xa34737, 0x852c5e, 0x592242, 0x261b23],
                                            [0x212629, 0x067778, 0x49b8a8, 0x85edb6, 0xd9e5cd],
                                            [0xdb5800, 0xff9000, 0xf0c600, 0x8ea106, 0x59631e],
                                            [0x6b6258, 0x85796d, 0xb5b5b5, 0x2b2b2b, 0x262626],
                                            [0xeb5902, 0xe94211, 0xe83503, 0xc81e6c, 0x732370, 0x46318e, 0x045697, 0x00847f]];
                    
                    var billBoard:Sprite = new Sprite();
                    
                    billBoard.graphics.beginFill(0x000000, 1);             
                    billBoard.graphics.drawRect(0, 0, model[3], model[4]);
                    billBoard.graphics.endFill();             
                    
                    var num:Number = model[3] / 10;
                    var colorRand:int = int(colours.length * Math.random());
                    
                    for (i = 0; i < model[3]; i+=10) 
                    {                       
                        for (j = 0; j < model[4]; j+=10)
                        {
                            if(i == 0 || j == 0 || i == model[3] - 10 || j == model[4] - 10)
                            {
                                if((i + j)/10 % 2 == 0)
                                {
                                    var mat:Matrix = new Matrix();
                                    var colors:Array = [0xFFFFFF, colours[colorRand][int(colours[colorRand].length * Math.random())]];
                                    var alphas:Array =[Math.random() + .2,0];
                                    var ratios:Array =[0, 255];
                                    mat.createGradientBox(10,10,0,i,j);

                                    billBoard.graphics.beginGradientFill(GradientType.RADIAL,colors,alphas,ratios,mat);
                                    billBoard.graphics.drawCircle(i + 5, j + 5, 5);
                                    billBoard.graphics.endFill();
                                }                                
                            }
                            else
                            {
                                billBoard.graphics.beginFill(colours[colorRand][int(colours[colorRand].length * Math.random())], .4);             
                                billBoard.graphics.drawRect(i, j, 9, 9);
                                billBoard.graphics.endFill();    
                            }
                        }
                    }   
                                       
                    billBoard.x = model[1];
                    billBoard.y = model[2];
                    backgroundS.addChild(billBoard);                    

                    backgroundS.graphics.endFill();
                    
                }
            }
    
            var b:BitmapData = new BitmapData(1024, 1024, true, 0x0);
            b.draw(backgroundS);
            var bitmap:Bitmap = new Bitmap(b);
           
            return [backgroundS, bitmap];
        }
        
        public function buildEnemies():Sprite
        {
            constructedEnemies = [];
            var enemySprites:Sprite = new Sprite();
            
            for each (var model:Object in enemies)
            {
                var enemy:GeneralObstacle = enemyProducer(model);
                constructedEnemies.push(enemy);
                enemySprites.addChild(enemy);
            }
            
            return enemySprites;
        }
        
        public function enemyProducer(configuration:Object):GeneralObstacle
        {
            var enemy:GeneralObstacle;
            switch(configuration.type)
            {
                case 1:
                    enemy = new ChainSaw(configuration);
                    break;
                case 2:
                    enemy = new Mace(configuration);
                    break;
                case 3:
                    enemy = new ElectricCharge(configuration);
                    break;
            }
            
            enemy.x = configuration.x;
            enemy.y = configuration.y;
            enemy.draw();
            
            return enemy;
        }
        
        public function update():void
        {
            for each (var enemy:GeneralObstacle in constructedEnemies)
            {
                enemy.update();
            }
        }
    }

}
